package com.laikan.reader.bookfriend.requester;

import android.content.Context;
import android.widget.ImageView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.laikan.library.httpvolley.engine.HttpApiRequestEngine;
import com.laikan.library.httpvolley.engine.Requester;
import com.laikan.library.httpvolley.helper.RequestListener;
import com.laikan.library.httpvolley.helper.VolleyErrorHandler;
import com.laikan.library.httpvolley.response.HttpResponse;
import com.laikan.library.managers.ToastMgr;
import com.laikan.library.utils.SPUtil;
import com.laikan.reader.R;
import com.laikan.reader.comment.APIProtocol;
import com.laikan.reader.comment.Constants;
import com.laikan.reader.core.LaikanApplication;
import com.laikan.reader.core.bean.BaseDataBean;
import com.laikan.reader.core.bean.SignInBean;
import com.laikan.reader.core.bean.SignInSuccessBean;
import com.laikan.reader.core.events.LaiKanEvent;
import com.socks.library.KLog;

import java.lang.reflect.Type;
import java.util.HashMap;

/**
 * Created by Administrator on 2016/10/27.
 */
public class SigninRequester {

    public static final int SIGNIN_REQUEST_BOOKSHELF = 0;
    public static final int SIGNIN_REQUEST_SIGNPAGE = 1;

    /**
     * 获取签到信息
     */
    public static void requestData(Context context, final int from) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("sd", SPUtil.getString("token", ""));
        map.put("site", "3");
        map.put("signinTime", "6");

        RequestListener requestListener = new RequestListener() {
            @Override
            public void onPreExecute() { KLog.e("---------volley onPreExecute!!!"); }

            @Override
            public void onSuccess(HttpResponse httpResponse) {
                try {
                    BaseDataBean<SignInBean> result;
                    Gson gson = new Gson();
                    Type type = new TypeToken<BaseDataBean<SignInBean>>() {
                    }.getType();
                    result = new BaseDataBean<SignInBean>();
                    result = gson.fromJson(httpResponse.responseContent, type);
                    if (result != null) {
                        if (null == result.getData() || !Constants.RESULT_OK.equals(result.getResult())) {
                            if (SigninRequester.SIGNIN_REQUEST_BOOKSHELF != from) {
                                ToastMgr.showShortToast(result.getError_msg());
                            }
                        }else {
                            LaiKanEvent laiKanEvent = LaiKanEvent.getInstance();
                            laiKanEvent.setEventID(LaiKanEvent.EVENT_ID_SIGNIN_DATAREQUEST);
                            laiKanEvent.setElves(result);
                            LaikanApplication.postEvent(laiKanEvent);
                        }
                    }else {
                        if (SigninRequester.SIGNIN_REQUEST_BOOKSHELF != from) {
                            ToastMgr.showShortToast(R.string.laikan_data_load_failed);
                        }
                    }

                }catch (Exception e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(VolleyErrorHandler errorHandler) {
                if (SigninRequester.SIGNIN_REQUEST_BOOKSHELF != from) {
                    ToastMgr.showShortToast(R.string.laikan_network_anomaly);
                }
            }

            @Override
            public void onFinish() {

            }
        };

        Requester requester = new Requester();
        requester.setUrl(APIProtocol.getRootURL2() + "signin?sd=" + SPUtil.getString("token", "") + "&site=3");
//        requester.setMapBody(map);
        HttpApiRequestEngine.getInstance().httpGetRequest(requester, requestListener);

//        HttpApiRequestEngine.getInstance().httpGetRequest(APIProtocol.getRootURL2() + "signin?sd=" + SPUtil.getString("token", "") + "&site=3", null, null, requestListener);
    }

    /**
     * 签到
     * @param img
     * @param today
     * @param status
     */
    public static void signInRequest(final ImageView img, int today, final int status) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("sd", SPUtil.getString("token", ""));
        map.put("site", "3");
        map.put("signinTime", today + "");
        RequestListener requestListener = new RequestListener() {
            @Override
            public void onPreExecute() { KLog.e("---------volley onPreExecute!!!"); }

            @Override
            public void onSuccess(HttpResponse httpResponse) {
//                signInPopupWindow.setContentView("66", "6+60");
//                signInPopupWindow.showAtLocation(signin_rootview, Gravity.CENTER, 0, 0);
                try {
                    BaseDataBean<SignInSuccessBean> result;
                    Gson gson = new Gson();
                    Type type = new TypeToken<BaseDataBean<SignInSuccessBean>>() {
                    }.getType();
                    result = gson.fromJson(httpResponse.responseContent, type);
                    if (result != null) {
                        if (null == result.getData() || !Constants.RESULT_OK.equals(result.getResult())) {
                            ToastMgr.showShortToast(result.getError_msg());
                        }else {
                            //签到设置状态
                            LaiKanEvent laiKanEvent = LaiKanEvent.getInstance();
                            laiKanEvent.setEventID(LaiKanEvent.EVENT_ID_SIGNIN_SIGNREQUEST);
                            laiKanEvent.setElves(result);
                            LaikanApplication.postEvent(laiKanEvent);
                        }
                    }else {
                        ToastMgr.showShortToast(R.string.laikan_network_anomaly);
                    }

                }catch (Exception e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(VolleyErrorHandler errorHandler) {
            }

            @Override
            public void onFinish() {

            }
        };

        Requester requester = new Requester();
        requester.setUrl(APIProtocol.laikan_daily_signin + "/mark");
        requester.setMapBody(map);
        HttpApiRequestEngine.getInstance().httpPostRequest(requester, requestListener);

//        HttpApiRequestEngine.getInstance().httpPostRequest(APIProtocol.laikan_daily_signin + "/mark", map, null, null, requestListener);
    }
}
